unit untRelPadrao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,  ExtCtrls, DB, IBCustomDataSet, IBQuery, ppEndUsr,
  ppDB, ppDBPipe, ppParameter, ppCtrls, ppBands, ppClass, ppReport,
  ppStrtch, ppSubRpt, ppPrnabl, ppCache, ppComm, ppRelatv, ppProd, Menus,
  cxLookAndFeelPainters, dxBar,  dxBarDBNav, StdCtrls,
  cxButtons, ActnList, ImgList, cxGraphics,  jpeg, dxmdaset, ppVar,
  cxLookAndFeels, ACBrBase, ACBrEnterTab;

type
  TfrmRelPadrao = class(TForm)
    ActionList1: TActionList;
    cxImageList1: TcxImageList;
    ActOK: TAction;
    ActCancelar: TAction;
    ActConfigurar: TAction;
    RzPanel1: TPanel;
    RzPanel2: TPanel;
    RzPanel3: TPanel;
    cxButton1: TcxButton;
    cxButton2: TcxButton;
    cxButton3: TcxButton;
    edtRel: TIBDataSet;
    dtEditRel: TDataSource;
    dsLstRel: TDataSource;
    DtlstRel: TIBQuery;
    dsFiltro: TDataSource;
    mtbFiltro: TdxMemData;
    dsDadosLicenca: TDataSource;
    dtLstLicenca: TIBQuery;
    dtLstLicencaCODIGO: TIntegerField;
    dtLstLicencaNOME: TIBStringField;
    dtLstLicencaCPF_CNPJ: TIBStringField;
    dtLstLicencaLOGRADOURO: TIBStringField;
    dtLstLicencaNUMERO: TIBStringField;
    dtLstLicencaCOMPLEMENTO: TIBStringField;
    dtLstLicencaBAIRRO: TIBStringField;
    dtLstLicencaCIDADE: TIBStringField;
    dtLstLicencaUF: TIBStringField;
    dtLstLicencaCEP: TIBStringField;
    dtLstLicencaCONTADOR: TIntegerField;
    dtLstLicencaCRC: TIBStringField;
    dtLstLicencaFONE: TIBStringField;
    dtLstLicencaEMAIL: TIBStringField;
    dtLstLicencaLOGOTIPO: TBlobField;
    ACBrEnterTab1: TACBrEnterTab;
    dtLstLicencaLICENCA: TIBStringField;
    procedure ActCancelarExecute(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure rptRelBeforePrint(Sender: TObject);
  private
    { Private declarations }
  protected
    sqloriginal : string;
    RptFiltro: string;
    Relatorio, Designer : integer; //indice do componete de relatorio que armazenara o leiaute
    procedure SetaHeader;
    procedure HabilitaHeader(Sender: TObject);
    procedure ConfigRel(Rel: TppReport; Designer :TppDesigner);

  public
    { Public declarations }
  end;

var
  frmRelPadrao: TfrmRelPadrao;

implementation

{$R *.dfm}

procedure TfrmRelPadrao.ActCancelarExecute(Sender: TObject);
begin
  close;
end;

procedure TfrmRelPadrao.FormShow(Sender: TObject);
var
 i : integer;
begin
  mtbFiltro.Open;
  mtbFiltro.Append;

  sqloriginal := DtlstRel.SQL.Text;
  dtLstLicenca.Close;
  dtLstLicenca.Open;

  For i := 0 to ComponentCount - 1 do
  begin
     if ( Components[I] is TppReport) then
     begin
       if (TppReport(Components[I]).Tag = 1) then
         Relatorio := i;
     end
     else if ( Components[I] is TppDesigner ) then
     begin
       if (TppDesigner(Components[I]).Tag = 1) then
         Designer := i;
     end
  end;

end;

procedure TfrmRelPadrao.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  DtlstRel.Close;
  DtlstRel.SQL.Text := sqloriginal;
end;

procedure TfrmRelPadrao.rptRelBeforePrint(Sender: TObject);
begin
  //lblFiltro.Caption := RptFiltro;
end;

procedure TfrmRelPadrao.HabilitaHeader(Sender: TObject);
begin
  TppHeaderBand(Sender).visible := true;
end;

procedure TfrmRelPadrao.SetaHeader;
var
  i : integer;
begin
  for i := 0 to frmRelPadrao.ComponentCount - 1 do
  begin
    if (frmRelPadrao.Components[I] is TppHeaderBand) then
       TppHeaderBand(frmRelPadrao.Components[I]).BeforePrint := HabilitaHeader;
  end;
end;

procedure TfrmRelPadrao.ConfigRel(Rel: TppReport; Designer :TppDesigner);
begin
  edtRel.close;
  edtRel.parambyname('codigo').value := 1;
  edtRel.Open;

  Rel.Template.DatabaseSettings.Name := edtRel.fieldbyname('CODIGO').asstring;
  Rel.Template.LoadFromDatabase;
  Designer.Show;
  edtRel.edit;

  edtRel.Transaction.CommitRetaining;
end;

end.
